package com.macro.mall.product.controller;

import com.github.pagehelper.PageInfo;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.PmPrpty;
import com.macro.mall.product.dto.PmPrptyDto;
import com.macro.mall.product.service.PmPrptyService;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URLDecoder;
import java.util.List;

/**
 * 属性管理Controller
 * Created by macro on 2018/4/26.
 */
@Slf4j
@Controller
@Api(tags = "PmPrptyController", description = "属性管理")
@RequestMapping("/prpty")
public class PmPrptyController {

    @Autowired
    private PmPrptyService pmPrptyService;
    @Autowired
    private Mapper mapper;

    @ApiOperation(value = "分页获取属性列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmPrptyDto>> list(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                          @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                          @RequestParam(value = "prptyName", defaultValue = "") String prptyName
    ) {

        prptyName=URLDecoder.decode(prptyName);
        log.debug("★★★★★★PmPrptyController.list   pageNum="+pageNum+" pageSize="+pageSize+"  prptyName="+prptyName);
        PmPrptyDto prptyDto = new PmPrptyDto();
        prptyDto.setPrptyName(prptyName);
        List<PmPrpty> prptyList = pmPrptyService.listPrpty(pageNum,pageSize,prptyDto);
        PageInfo<PmPrpty> pageInfo = new PageInfo<PmPrpty>(prptyList);

        List<PmPrptyDto> prptyDtoList = pmPrptyService.converDataList(prptyList);
        CommonPage<PmPrptyDto> result = new CommonPage<PmPrptyDto>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setTotalPage(pageInfo.getPages());
        result.setList(prptyDtoList);
        return CommonResult.success(result);
    }
    @ApiOperation(value = "分页获取属性列表")
    @RequestMapping(value = "/listByPackageId", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<PmPrptyDto>> listByPackageId(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                     @RequestParam(value = "prptyName", defaultValue = "") String prptyName,
                                                     @RequestParam(value = "prptyPackageId", defaultValue = "") Long prptyPackageId
    ) {

        prptyName=URLDecoder.decode(prptyName);
        log.debug("★★★★★★PmPrptyController.list   pageNum="+pageNum+" pageSize="+pageSize+"  prptyName="+prptyName +"  prptyPackageId ="+prptyPackageId);
        PmPrptyDto prptyDto = new PmPrptyDto();
        if(!StringUtils.isBlank(prptyName)) {
            prptyDto.setPrptyName(prptyName);
        }
        List<PmPrpty> prptyList = pmPrptyService.listPrptyByPackageId(pageNum,pageSize,prptyDto,prptyPackageId);
        PageInfo<PmPrpty> pageInfo = new PageInfo<PmPrpty>(prptyList);

        List<PmPrptyDto> prptyDtoList = pmPrptyService.converDataList(prptyList);
        CommonPage<PmPrptyDto> result = new CommonPage<PmPrptyDto>();
        result.setPageNum(pageInfo.getPageNum());
        result.setPageSize(pageInfo.getPageSize());
        result.setTotal(pageInfo.getTotal());
        result.setTotalPage(pageInfo.getPages());
        result.setList(prptyDtoList);
        return CommonResult.success(result);
    }
    @ApiOperation(value = "获取单个属性")
    @RequestMapping(value = "/getPrptyById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmPrptyDto> getPrptyById(@RequestParam(value = "prptyId", defaultValue = "") Long prptyId
    ) {
        PmPrpty prpty = pmPrptyService.getById(prptyId);
        PmPrptyDto pmPrptyDto= mapper.map(prpty,PmPrptyDto.class);
        return CommonResult.success(pmPrptyDto);
    }
    @ApiOperation(value = "保存属性")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult save(@Validated @RequestBody PmPrptyDto pmPrptyDto, BindingResult result) {
        CommonResult commonResult;
        try {
             pmPrptyService.checkPmPrpty(pmPrptyDto);
        }catch (Exception e){
            commonResult=CommonResult.failed(e.getMessage());
            return commonResult;
        }
        int count = pmPrptyService.savePmPrpty(pmPrptyDto);
        if (count == 1) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "删除属性")
    @RequestMapping(value = "/deletePrpty", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deletePrpty(@RequestParam(value = "prptyIds") String prptyIds) {
        CommonResult commonResult;
        int count = pmPrptyService.deletePrpty(prptyIds);
        if (count > 0) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
    @ApiOperation(value = "删除属性值")
    @RequestMapping(value = "/deletePrptyVal", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deletePrptyVal(@RequestParam(value = "prptyValIds") String prptyValIds) {
        CommonResult commonResult;
        int count = pmPrptyService.deletePrptyVal(prptyValIds);
        if (count >1) {
            commonResult = CommonResult.success(count);
        } else {
            commonResult = CommonResult.failed();
        }
        return commonResult;
    }
}
